package com.gulimall.cart.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.gulimall.api.feign.ProductClient;
import com.gulimall.cart.service.ICartService;
import com.gulimall.common.core.domain.Result;
import com.gulimall.common.dto.cart.CartItemDTO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.gulimall.common.constant.RedisConstants.CART_SKUINFO;

@Service
public class CartServiceImpl implements ICartService {
    @Resource
    private ProductClient productClient;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 把购物车信息存入redis
     */
    @Override
    public void setCartInfo(Long skuId) {
        Result<CartItemDTO> result = productClient.getSkuInfo(skuId);
        CartItemDTO cartItemDTO = result.getData();
        stringRedisTemplate.opsForValue().set(CART_SKUINFO, JSONObject.toJSONString(cartItemDTO));
    }
}
